package com.songlanyun.modules.signin.controller.api;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.signin.service.SignInRecordService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * 签到管理-签到记录
 *
 * @author lijiahe
 * @date 2022-08-03
 */
@RestController
@RequestMapping("api/signInRecord")
public class SignInRecordApiController extends AbstractApiController {

    @Autowired
    private SignInRecordService signInRecordService;

    @PostMapping("/signInNow")
    // @RequiresPermissions("signIn:record:signIn")
    public R signInNow(@RequestParam Map<String, Object> params) {
        LocalDateTime now = null;
        String day = MapUtil.getStr(params, "day");
        if (ObjectUtil.isNull(day)) {
            now = LocalDateTime.now();
        } else {
            now = LocalDateTime.parse(day, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }

        return R.ok().put("data", signInRecordService.signInNow(getUserId(true), now));
    }

    @GetMapping("/dataStatistics/{month}")
    // @RequiresPermissions("signIn:record:list")
    public R dataStatistics(@PathVariable("month") String month) {
        Long userId = getUserId(true);
        return R.ok().put("list", signInRecordService.dataStatistics(userId, month));
    }

    @GetMapping("/list/{month}")
    // @RequiresPermissions("signIn:record:list")
    public R list(@PathVariable("month") String month) {
        Long userId = getUserId(true);
        return R.ok().put("list", signInRecordService.listSignInRecord(userId, month));
    }

}
